var linkurl =  "http://"+window.location.host+"/" ;//获取当前域名
var page = 1;
var countpage=1;
var indexs = ["①","②","③","④","⑤","⑥","⑦","⑧","⑨","⑩"];
var t = n = 0, count;
var index = 0;
var auto_timer;
//广告加载
$(document).ready(function () {
    //登录判断
    $.ajax({
        type : "get",
        url : linkurl + "isLoged",
        data: {},
        dataType: 'json',
        success:function (Result) {
            var result = Result.result;
            if(result==1){
                $(".pcc_gerenbnt").removeClass("hide");
                $("#to-login").addClass("hide");
                $("#to-reg").addClass("hide");
            }
        }
    });

    // 分页按钮加载结束
    $.ajax({
        type: 'get',
        url : linkurl + "getAdList",
        data: {pid:1},
        dataType: 'json',
        success:function (Result) {
            var data = Result.data;
            var html="";
            $.each(data,function (index,item) {
                html=html+"<a class='item2 rel' target='_blank' href='//"+item.linkUrl+"'>" +
                    "<img class='banner-img' src='"+linkurl+"images/ad/"+item.img+"'>" +
                    "<div class='title-mask abs'></div>" +
                    "<div class='title abs fff' style='font-size:26px;'>"+item.adTitle+"</div>" +
                    "</a>";
            })
            $("#banner_list").html(html)
        }
    });
    count = 3;
    for(var i = 1;i<count;i++){$("#banner").append("<li></li>");}
    $("#banner_list a:not(:first-child)").hide();
    $("#banner_info").html($("#banner_list a:first-child").find("img").attr('alt'));
    $("#banner_info").click(function () {
        window.open($("#banner_list a:first-child").attr('href'), "_blank")
    });
    $("#banner li").on('click', function () {
        var i = $(this).index();//获取Li元素内的值，即1，2，3，4
        n = i;
        if (i >= count) return;
        $("#banner_info").html($("#banner_list a").eq(i).find("img").attr('alt'));
        $("#banner_info").unbind().click(function () {
            window.open($("#banner_list a").eq(i).attr('href'), "_blank")
        });
        $("#banner_list a").filter(":visible").hide().parent().children().eq(i).show();
        document.getElementById("banner").style.background = "";
        $(this).toggleClass("on");
        $(this).siblings().removeAttr("class");
    });
    t = setInterval("showAuto()", 3000);
    $("#banner").hover(function () {
        clearInterval(t)
    }, function () {
        t = setInterval("showAuto()", 3000);
    });
    //本站最新发布
    $.ajax({
        type : "get",
        url : linkurl + "getzuixinXunrenList",
        data: {pagesize:10},
        dataType: 'json',
        success:function (Result) {
            var data = Result.data;
            $("#live_radio_list").html("<div class='live_radio_listtitle'><h3>本站最新发布</h3></div>");
            $.each(data, function (index,item) {
                if(index<=2){
                    $("#live_radio_list").append("<a href='"+linkurl+item.url+"/"+item.linkUrl+"' class='live_xunren_list'><span class='pcc-colorff8420 pcc_padding10'>"+indexs[index]+"</span>["+item.provinceName+"] "+item.title+" </a>");
                }else{
                    $("#live_radio_list").append("<a href='"+linkurl+item.url+"/"+item.linkUrl+"' class='live_xunren_list'><span class='pcc-colore0dbdb pcc_padding10'>"+indexs[index]+"</span>["+item.provinceName+"] "+item.title+" </a>");
                }
            })
        }
    });
    $.ajax({
        type: 'get',
        url : linkurl + "getXunrenClassifyList",
        data: {},
        dataType: 'json',
        success: function (Result) {
            var  data = Result.data;
            $("#pcc-classify").html("");
            $.each(data, function (index,item) {
                $("#pcc-classify").append("<a href='"+linkurl+"d-"+item.id+"/'>"+item.className+"")
            })
        }
    });
});
var url =window.location.href;
var urllenght=url.split("d-").length;
var urllenght2=url.split("a-").length;
var urllenght4=url.split("b-").length;
var urllenght5=url.split("c-").length;
var urllenght6=url.split("-").length;
//urllenght6==2,
var urllenght1=url.split("_").length;
var urllenght3=url.split("/").length;
var provinceName="";
var i = document.getElementsByTagName("meta");
//当前位置省市区，分类
var className="";
var provinceName1="";
var cityName1="";
var countyName1="";
//http://localhost:8121/Found/d-2/
var allName =url.split("/")[urllenght3-2];

//只有分类
if(urllenght==2&&urllenght4==1&&urllenght2==1&&urllenght5==1){
    var str =url.split("-")[1];
    var classifyID = str.substring(0,str.length-1);
    var provinceID = 0;
}else if(urllenght2==2&&urllenght1==1&&urllenght4==1&&urllenght==1){
    //http://localhost:8121/Found/a-anhui/ 只有省
    var classifyID =0;
    var str =url.split("-")[1];
    var provinceID = getID(str.substring(0,str.length-1));
}else if(urllenght4==2&&urllenght==1&&urllenght5==1&&urllenght==1){
    var classifyID =0;
    var str =url.split("-")[2];
    //http://localhost:8121/Found/a-anhui_b-huaibei/ 只有省市
    //alert(getID(url.split("-")[2]));
    var provinceID = getID(str.substring(0,str.length-1));
}else if(urllenght5==2&&urllenght==1){
    var classifyID =0;
    //http://localhost:8121/Found/a-anhui_b-huaibei_c-jj 只有省市区
    //alert(getID(url.split("-")[2]));
    var str =url.split("-")[3];
    var provinceID = getID(str.substring(0,str.length-1));
    //省分类http://localhost:8121/Found/a-anhui_d-1/
}else if(urllenght==2&&urllenght2==2&&urllenght4==1&&urllenght5==1){
     var diquName_yiji=url.split("-")[1];
     var diquname_sanji=diquName_yiji.split("_")[0];
     var provinceID = getID(diquname_sanji);
     var str =url.split("-")[2];
     var classifyID =str.substring(0,str.length-1);
     //省市分类http://localhost:8121/Found/a-anhui_b-huaibei_d-2/
}else if(urllenght==2&&urllenght2==2&&urllenght4==2&&urllenght5==1){
    var diquName_yiji=url.split("-")[2];
    var diquname_sanji=diquName_yiji.split("_")[0];
    var provinceID = getID(diquname_sanji);
    var str =url.split("-")[3];
    var classifyID =str.substring(0,str.length-1);
    //http://localhost:8121/Found/a-anhui_b-huaibei_c-suntuan_d-2/
}else if(urllenght==2&&urllenght2==2&&urllenght4==2&&urllenght5==2){
    var diquName_yiji=url.split("-")[3];
    var diquname_sanji=diquName_yiji.split("_")[0];
    var provinceID = getID(diquname_sanji);
    var str =url.split("-")[4];
    var classifyID =str.substring(0,str.length-1);
}else{
    classifyID=0;
    provinceID=0;
}
    function showAuto() {
    n = n >= (count - 1) ? 0 : ++n;
    //console.log("n="+n)
    $("#banner li").eq(n).trigger('click');
}
    function autoChangeItem() {
    index++;
    var length = $("#live_radio_list").find('.broadcast').length;
    var i = index % length;
    $("#live_radio_list").find('.broadcast').removeClass('hide').addClass('hide');
    $("#live_radio_list").find('.broadcast').eq(i).removeClass('hide');
}
    $(function () {
       //右侧导航点击回到头部
        $("#sidebar-c").click(function(){
           $('body,html').animate({scrollTop:0},1000);
           return false;
        });
        xunrenlist(page,classifyID,provinceID)
             // 分页按钮加载
        $("#demo1").paginate({
                count       : countpage,
                start       : 1,
                display     : 10,
                border                  : true,
                border_color            : '#cecece',
                text_color              : '#666666',
                background_color        : '#ffffff',    
                border_hover_color      : '#ff8420',
                text_hover_color        : 'ff8420',
                background_hover_color  : '#ffffff', 
                rotate      : false,
                images      : false,
                mouse       : 'press'
            });

        //当前位置
         if(classifyID==0 && provinceID==0){
             $('#pcc_liebiao').html("");
             $('#pcc_liebiao').append("寻人启事>寻人列表");
            }
        else if(classifyID==0 && provinceID!=0){
            $('#pcc_liebiao').html("");
            $('#pcc_liebiao').append("寻人启事>");
            getOneDiqu(provinceID);
            if(provinceName1!=""){
                $('#pcc_liebiao').append(provinceName1+"寻人>");
            }
            if(cityName1!=""){
                $('#pcc_liebiao').append(cityName1+"寻人>");
            }
            if(countyName1!=""){
                $('#pcc_liebiao').append(countyName1+"寻人>");
            }
            $('#pcc_liebiao').append("寻人列表");
        }else if(classifyID!=0 && provinceID!=0){
            $('#pcc_liebiao').html("");
            $('#pcc_liebiao').append("寻人启事>");
            $.ajax({
                type: 'get',
                url : linkurl + "getxunrenClassifyname",
                data: {id : classifyID},
                dataType: 'json',
                async:false,
                success:function (Result) {
                    var data = Result.data;
                    className = data.className;
                    $('#pcc_liebiao').append(className+">");
                }
            })
            getOneDiqu(provinceID);
            if(provinceName1!=""){
                $('#pcc_liebiao').append(provinceName1+"寻人>");
            }
            if(cityName1!=""){
                $('#pcc_liebiao').append(cityName1+"寻人>");
            }
            if(countyName1!=""){
                $('#pcc_liebiao').append(countyName1+"寻人>");
            }
            $('#pcc_liebiao').append("寻人列表");
        }else if(classifyID!=0){
            $('#pcc_liebiao').html("");
            $('#pcc_liebiao').append("寻人启事>");
            $.ajax({
                type: 'get',
                url : linkurl + "getxunrenClassifyname",
                data: {id : classifyID},
                dataType: 'json',
                async:false,
                success:function (Result) {
                    var data = Result.data;
                    className = data.className;
                    $('#pcc_liebiao').append(className+">");
                    i[1]["content"] = data.seoKeywords;
                    i[2]["content"] = data.seoDescription;
                    $(document).attr("title",data.seoTitle);//修改title值
                }
            })
            $('#pcc_liebiao').append("寻人列表");

        }
       


        $("#pcc-logout").click(function () {
            $.ajax({
                type : "post",
                url : linkurl + "logout",
                data: {},
                dataType: 'json',
                success:function () {
                    $(".pcc_gerenbnt").addClass("hide");
                    $("#to-login").removeClass("hide");
                    $("#to-reg").removeClass("hide");
                }
            })
        });
        $(".jPag-pages li").click(function(){
	          var page = $(this).text();
             xunrenlist(page,classifyID,provinceID)
	    })
	    $(".search_button3").click(function () {
		    var search = $("#search_key").val();
	 
		    if(search!=""){
		        window.location.href = linkurl+"sousuojieguo.html?search=" + search;
		    }
		});

    })
    function xunrenlist(page,classifyID,provinceID){
	     $.ajax({
            type : "get",
            url : linkurl + "selectxunenlist",
            data: {page:page,pagesize:4,classifyID:classifyID,provinceID:provinceID},
            dataType: 'json',
             async:false,
            success:function (Result) {
                if(page == 1){$("#pcc-list").html("");}
                var data = Result.data;
                var html="";
                $.each(data,function (index,item) {
                    html=html+("<a class='article-list cursor' target='_blank' href='"+linkurl+""+item.url+"/"+item.linkUrl+"'>" +
                        "<div class='article1 rel bb'>" +
                        "<div class='title1 hover c3333'>["+item.provinceName+"·"+item.cityName+"·"+item.countyName+"]"+item.title+"</div>" +
                        "<div class='author1 c333'>寻找人："+item.contact+" <em class='bbb'>"+dateFormatUtil(item.createTime*1000)+"</em></div>" +
                        "<div class='summary1'>"+item.content+"</div>" +
                        "</div>" +
                        "</a>")
                })
                $("#pcc-list").html(html);
                if(Result.result!=0){
                    countpage= data[0].total;
                }
                var provinceName="";
                var shiName="";
                var quName="";
                var types=1;
                var dd="";
                $.ajax({
                    type : "get",
                    url : linkurl + "getOneDiqu",
                    data: {id:provinceID},
                    dataType: 'json',
                    async:false,
                    success:function (Result) {
                        var data =Result.data;
                        if(Result.result!=0){
                            if(data.types==1){
                                types=2;
                            }else if(data.types==2){
                                types=3;
                            }else if( data.types==3){
                                types=1;
                                provinceID=0;
                            }

                           i[1]["content"] = data.seoKeywords;
                           i[2]["content"] = data.seoDescription;
                           $(document).attr("title",data.seoTitle);//修改title值*/

                        }
                        //types=1,id=1
                        $.ajax({
                            type : "get",
                            url : linkurl + "getindexDiquListByType",
                            data: {types:types,pid:provinceID},
                            dataType: 'json',
                            async:false,
                            success:function (Result) {
                                var data = Result.data;
                                $("#pcc-foot-c").html("");
                                $.each(data,function (index,item) {
                                    if(classifyID==0){
                                        if(types==2){
                                            dd=allName+"_b-"
                                        }else if(types==3){
                                            dd=allName+"_c-"
                                        }else{
                                            dd="a-"
                                        }
                                        $("#pcc-foot-c").append("<a class='pcc_fooldiquxunren' href='"+linkurl+""+dd+""+item.url+"/'>"+item.diquName+"寻人</a>")
                                    }else{
                                        if(types==1){
                                            //alert(allName);
                                            if(allName.split("_").length==4){
                                                dd="a-"+item.url;
                                            }else{
                                                dd="a-"+item.url+"_"+allName
                                            }

                                        }else if(types==2){
                                            //alert(allName);
                                            var sheng=allName.split("_")[0];
                                            var calss=allName.split("_")[1];
                                            dd=sheng+"_b-"+item.url+"_"+calss;
                                        }else{
                                            //alert(allName);
                                            var sheng=allName.split("d-")[0];
                                            var calss=allName.split("_")[2];
                                            dd=sheng+"c-"+item.url+"_"+calss;
                                        }
                                        $("#pcc-foot-c").append("<a class='pcc_fooldiquxunren' href='"+linkurl+""+dd+"/'>"+item.diquName+"寻人</a>")

                                    }
                                })
                            }
                        });
                        $.ajax({
                            type : "get",
                            url : linkurl + "selectxunenlist",
                            data: {page:1,pagesize:4,classifyID:classifyID,provinceID:provinceID},
                            dataType: 'json',
                            success:function (Result) {
                                var data = Result.data;
                                $("#pcc-foot-x").html("");
                                $.each(data, function (index,item) {
                                    if(types==1){
                                        $.ajax({
                                            type: 'get',
                                            url : linkurl + "getOneDiqu",
                                            data: {id : item.province},
                                            dataType: 'json',
                                            async:false,
                                            success:function (Result) {
                                                var data = Result.data;
                                                provinceName = data.diquName;
                                            }
                                        })
                                    }else if(types==2){
                                        $.ajax({
                                            type: 'get',
                                            url : linkurl + "getOneDiqu",
                                            data: {id : item.city},
                                            dataType: 'json',
                                            async:false,
                                            success:function (Result) {
                                                var data = Result.data;
                                                provinceName = data.diquName;
                                            }
                                        })
                                    }else if (types==3){
                                        $.ajax({
                                            type: 'get',
                                            url : linkurl + "getOneDiqu",
                                            data: {id : item.county},
                                            dataType: 'json',
                                            async:false,
                                            success:function (Result) {
                                                var data = Result.data;
                                                provinceName = data.diquName;
                                            }
                                        })
                                    }
                                    $("#pcc-foot-x").append("<div class='pcc-articlefool' ><a class='live_xunren_list' href='"+linkurl+item.url+"/"+item.linkUrl+"'>["+provinceName+"]"+item.title+"</a>" +
                                        ""+item.content+"</div>");
                                });
                            }
                        });
                    }
                });
            }
        });
	
	}
    function dateFormatUtil(longTypeDate){
    var dateTypeDate = "";
    var date = new Date();
    date.setTime(longTypeDate);
    dateTypeDate += date.getFullYear();   //年
    dateTypeDate += "-" + getMonth(date); //月
    dateTypeDate += "-" + getDay(date);   //日
    return dateTypeDate;
}
    //返回 01-12 的月份值
    function getMonth(date){
    var month = "";
    month = date.getMonth() + 1; //getMonth()得到的月份是0-11
    if(month<10){
        month = "0" + month;
    }
    return month;
}

    //返回01-30的日期
    function getDay(date){
    var day = "";
    day = date.getDate();
    if(day<10){
        day = "0" + day;
    }
    return day;
}
    function GetQueryString(name) {
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(decodeURI(r[2])); return null;
}
    function getOneDiqu(provinceID){
    $.ajax({
        type: 'get',
        url : linkurl + "getDiqunNameList",
        data: {id : provinceID},
        dataType: 'json',
        async:false,
        success:function (Result) {
            var data =Result.data;
            var strlength=data.split(",").length;
            var str=data.split(",");
            if(strlength==1){
                provinceName1=str;
            }else if(strlength==2){
                provinceName1=str[0];
                cityName1=str[1];
            }else{
                provinceName1=str[0];
                cityName1=str[1];
                countyName1=str[2];
            }
        }
    })
}
    function getID(URL){
    var diquID="";
    $.ajax({
        type : "GET",
        url : linkurl + "getDiquIDbyurl",
        data: {url:URL},
        dataType: 'json',
        async:false,
        success:function (Result) {
            diquID=Result.data;
        }
    })
    return diquID;
}